<?php
require "vendor/autoload.php";
use QL\QueryList;
use Medoo\Medoo;
global $database;
$database = new medoo([
    'database_type' => 'mysql',
    'database_name' => 'php_crawl',
    'server' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8'
]);

function index(){
    echo "爬虫开始,敬请期待~~~~~~~~~~~~~~\n";
    #获取列表信息,设置循环次数
    $crawl_num=7;
    for($i=1;$i<=$crawl_num;$i++){
        echo "开始爬取第{$i}页内容\n";
        $url="http://blog.jobbole.com/category/php-programmer/page/{$i}/";
        echo "URL为{$url}\n";
        #设置爬虫规则
        $list_rule=[
            'article_title'=>['#archive .archive-title','text'],
            'content_url'=>['#archive .post-thumb > a:first-child','href'],
            'article_intro'=>['#archive .post-meta .excerpt','text'],
            'article_ctime'=>['#archive .post-meta > p:first-child','text','-a'],
            'article_thumb'=>['#archive .post-thumb > a > img','src']
        ];
        #获取爬虫数据
        $list_data=crawl_data($url,$list_rule);
        #爬取详情的数据
        foreach($list_data as $key=>$value){
            echo "抓取《{$value['article_title']}》内容 \n";
            #组合抓取详情规则
            $detail_rule=[
                'article_content'=>['.entry','html']
            ];
            #抓取数据
            $detail_data=crawl_data($value['content_url'],$detail_rule);
            #组合存入数据库的数据
            $db_data['article_title']=$value['article_title'];
            $db_data['article_content']= htmlspecialchars( $detail_data[0]['article_content']);
            $db_data['article_intro']=$value['article_intro'];
            $db_data['article_ctime']=find_time($value['article_ctime']);
            $db_data['article_thumb']=$value['article_thumb'];
            $db_data['article_uid']=random_uid();
            $db_data['article_views']=rand(100,100000);
            #插入数据库
            $res=$GLOBALS['database']->insert('article',$db_data);
            if(!$res){
                echo "写入《{$db_data['article_title']}》";
                echo "数据失败\n";
                var_dump('写入失败!');
                die;
            }else{
                echo "写入《{$db_data['article_title']}》";
                echo "数据成功\n";
            }
        }
        echo "爬虫程序完满执行成功~~~~\n";
        die;
    }
}
/** 爬取数据 */
 function crawl_data($url,$rule)
{
    $data=QueryList::query($url,$rule)->data;
    return $data;
}
/** 正则匹配日期时间 */
function find_time($string){
    $result=preg_match('/\d{4}\/\d{1,2}\/\d{1,2}/', $string, $matching);
    if($result){
        return $matching[0];
    }else{
        return "2017/10/26";
    }
}
/**随机生成UID */
function random_uid(){
    $uid_list=$GLOBALS['database']->select('user',["user_id"]);
    $max_key=sizeof($uid_list)-1;
    $min_key=0;
    $key=rand($min_key,$max_key);
    $uid_data=$uid_list[$key]['user_id'];
    return $uid_data;
}
index();